﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using BNPP.TemperatureConverter.Frameworks.MVVM;

namespace BNPP.TemperatureConverter.ViewModels
{
    [ExportViewModel("MainPage", false)]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class MainPageViewModel : ViewModel
    {
        public MainPageViewModel()
        {
            SetupProperties();
            EnDisable = new RelayCommand(() =>
            {
                isEnabled = !isEnabled;
                Action.RaiseCanExecuteChanged();
            });
            Action = new RelayCommand(() => MessageBox.Show("MainPage test"), () => isEnabled);
        }

        #region Properties
        private bool isEnabled = false;
        public RelayCommand EnDisable { get; set; }
        public RelayCommand Action { get; set; }

        private ObservablePropertyBacking<double> celsius;
        public Double Celsius
        {
            get { return celsius != null ? celsius.Value : 0; }
            set
            {
                celsius.Value = value;
                RaisePropertyChanged("Celsius");
            }
            
        }

        private ObservablePropertyBacking<double> farenheit = new ObservablePropertyBacking<double>();
        public Double Farenheit
        {
            get { return farenheit != null ? farenheit.Value : 0; }
            set
            {
                farenheit.Value = value;
                RaisePropertyChanged("Farenheit");
            }

        }

        //IDisposable subscription = Observable.FromEventPattern<PropertyChangedEventArgs>(this, "PropertyChanged")
        //  .Where(e => e.EventArgs.PropertyName == "Celsius")
        //  .Select(_ => this.Celsius)
        //  .Where(text => text > 3)
        //  //Subscribe will call LoadUser, no need for the extra Do(...)
        //  .Throttle(TimeSpan.FromSeconds(3))
        //  .SubscribeOn(Scheduler.Default)
        //  .ObserveOnDispatcher()
        //  .Subscribe(_ => this.Farenheit = _ / 5);  

        /*  Create property backing storage and subscribe UpdateDictionary to the observable 
            *  sequence. Since UpdateDictionary calls a web service, we throttle the sequence.
            */
        private void SetupProperties()
        {
            celsius = new ObservablePropertyBacking<double>();
            celsius.SubscribeOn(Scheduler.Default).ObserveOnDispatcher().Subscribe(UpdateDictionary);
        }

        private void UpdateDictionary(double text)
        {
            Farenheit = 9*(celsius.Value/5) + 32; 
            Debug.WriteLine(text);
        }
        #endregion

    }

    public class MainPageViewModelDesign : ViewModel
    {
        public MainPageViewModelDesign()
        {
            EnDisable = new RelayCommand(() =>
            {
                isEnabled = !isEnabled;
                Action.RaiseCanExecuteChanged();
            });
            Action = new RelayCommand(() => MessageBox.Show("demo1 test"), () => isEnabled);
        }

        #region Properties
        private bool isEnabled = false;
        public RelayCommand EnDisable { get; set; }
        public RelayCommand Action { get; set; } 
        #endregion

    }
}
